package com.socialsoftware.MediaCommunication.model;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Model class representing communication messages for WebRTC signaling
 */
public class CommunicationMessage {
    private String type;
    private String mode; // 通信模式：video, audio, screen
    private String fromUser;
    private String toUser;
    private String msg;
    private String sdp;
    private String sdpMid;
    private Integer sdpMLineIndex;
    private String candidate;

    public CommunicationMessage() {
    }

    public static CommunicationMessage fromJson(String jsonString) throws JSONException {
        JSONObject json = new JSONObject(jsonString);
        CommunicationMessage message = new CommunicationMessage();
        
        if (json.has("type")) message.setType(json.getString("type"));
        if (json.has("mode")) message.setMode(json.getString("mode"));
        if (json.has("fromUser")) message.setFromUser(json.getString("fromUser"));
        if (json.has("toUser")) message.setToUser(json.getString("toUser"));
        if (json.has("msg")) message.setMsg(json.getString("msg"));
        if (json.has("sdp")) message.setSdp(json.getString("sdp"));
        if (json.has("sdpMid")) message.setSdpMid(json.getString("sdpMid"));
        if (json.has("sdpMLineIndex")) message.setSdpMLineIndex(json.getInt("sdpMLineIndex"));
        if (json.has("candidate")) message.setCandidate(json.getString("candidate"));
        
        return message;
    }

    public JSONObject toJson() throws JSONException {
        JSONObject json = new JSONObject();
        
        if (type != null) json.put("type", type);
        if (mode != null) json.put("mode", mode);
        if (fromUser != null) json.put("fromUser", fromUser);
        if (toUser != null) json.put("toUser", toUser);
        if (msg != null) json.put("msg", msg);
        if (sdp != null) json.put("sdp", sdp);
        if (sdpMid != null) json.put("sdpMid", sdpMid);
        if (sdpMLineIndex != null) json.put("sdpMLineIndex", sdpMLineIndex);
        if (candidate != null) json.put("candidate", candidate);
        
        return json;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getFromUser() {
        return fromUser;
    }

    public void setFromUser(String fromUser) {
        this.fromUser = fromUser;
    }

    public String getToUser() {
        return toUser;
    }

    public void setToUser(String toUser) {
        this.toUser = toUser;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getSdp() {
        return sdp;
    }

    public void setSdp(String sdp) {
        this.sdp = sdp;
    }

    public String getSdpMid() {
        return sdpMid;
    }

    public void setSdpMid(String sdpMid) {
        this.sdpMid = sdpMid;
    }

    public Integer getSdpMLineIndex() {
        return sdpMLineIndex;
    }

    public void setSdpMLineIndex(Integer sdpMLineIndex) {
        this.sdpMLineIndex = sdpMLineIndex;
    }

    public String getCandidate() {
        return candidate;
    }

    public void setCandidate(String candidate) {
        this.candidate = candidate;
    }
} 